<template>
  <div class="person">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>管理员信息</span>
      </div>
      <div>
        <p>
          管理员ID:<span>{{ userInfo.id }}</span>
        </p>
        <el-divider></el-divider>
        <p>
          账号:<span>{{ userInfo.account }}</span>
        </p>
        <el-divider></el-divider>
        <p>
          用户组:<span>{{ userInfo.userGroup }}</span>
        </p>
        <el-divider></el-divider>
        <p>
          创建时间:<span>{{ userInfo.ctime | formatYMDhms }}</span>
        </p>
        <el-divider></el-divider>
        <div class="df">
          <p>管理员头像:</p>
          <el-upload
            class="avatar-uploader"
            :action="`${API}/users/avatar_upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="userInfo.imgUrl" :src="userInfo.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { updateUserAvatarReq } from "@/api/user";
export default {
  data() {
    return {
      //服务器地址
      API: "http://sell.h5.itsource.cn:8087",
    };
  },
  computed: {
    ...mapState("user", ["userInfo"]),
  },
  methods: {
    async handleAvatarSuccess(res) {
      console.log(res);
      let { code, imgUrl } = res;
      if (code === 0) {
        let res2 = await updateUserAvatarReq({
          imgUrl,
        });
        let { code } = res2.data;
        if (code === 0) {
          this.$store.dispatch("user/A_userInfo");
        }
      }
    },
    beforeAvatarUpload(file) {
      console.log(file);
      const isJPG = /(png|jpeg)/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是PNG JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.person {
  padding: 0 20px;
  margin-top: 15px;
}
span {
  font-weight: bold;
}
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  margin-left: 30px;
  margin-top: 30px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>